package com.zhuge_new.Activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.BottomSheetDialog
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.bumptech.glide.Glide
import com.idcard.cardocr.CaptureActivity
import com.idcard.cardocr.bean.IdCardInfo
import com.idcard.cardocr.common.CardType
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.zhuge_new.Base.BaseActivity
import com.zhuge_new.Base.putString
import com.zhuge_new.Base.toast
import com.zhuge_new.R
import com.zhuge_new.http.StringDialogCallback
import com.zhuge_new.share.BaseHttp
import com.zhuge_new.untils.ActivityStack
import kotlinx.android.synthetic.main.activity_realnamel.*
import java.util.*

/**
 * Created by lenovo on 2018-09-06.
 */
class RealNameActivity : BaseActivity(), View.OnClickListener {

    private val TYPE_ID_CARD_FRONT = 0
    private val TYPE_ID_CARD_BACK = 1
    private val TYPE_ID_CARD_FRONT1 = 11
    private val TYPE_ID_CARD_BACK1 = 12
    private var viewId = 0

    var selectList = ArrayList<LocalMedia>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_realnamel)
        init_title()
    }

    override fun init_title() {
        super.init_title()
        iv_frount.setOnClickListener(this)
        iv_revrse.setOnClickListener(this)
        ev_name.setOnClickListener(this)
        ev_number.setOnClickListener(this)
    }


    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.iv_frount -> {
                viewId = v!!.id
                showSeetDialog()
            }
            R.id.iv_revrse ->{
                viewId =v!!.id
                showSeetDialog()
            }
            R.id.btn_setp ->{
                certification()
            }
        }

    }

    private fun certification() {
        OkGo.post<String>(BaseHttp.certification_sub)
                .tag(this@RealNameActivity)
                .params("userName",ev_name.text.toString())
                .params("cardNo",ev_number.text.toString())
                .execute(object : StringDialogCallback(baseContext){
                    override fun onSuccessResponse(response: Response<String>, msg: String, msgCode: String) {
                        if (msgCode.equals(100)){
                            toast(msg)
                            putString("isPas","0")
                            ActivityStack.getScreenManager().popActivities(this@RealNameActivity::class.java)
                        }
                    }
                })
    }

    private fun showSeetDialog() {
        val view = LayoutInflater.from(baseContext).inflate(R.layout.select_photo_layout, null) as View
        val btnTakePhoto = view.findViewById(R.id.btn_take_photo) as Button
        val btnCancle = view.findViewById(R.id.btn_cancle) as Button
        val dialog = BottomSheetDialog(baseContext)

        btnTakePhoto.setOnClickListener {
            when (viewId) {
                R.id.iv_frount -> {
                    CaptureActivity.startAction(this, CardType.TYPE_ID_CARD_FRONT, BaseHttp.SDPath + "img", TYPE_ID_CARD_FRONT)
                    dialog.dismiss()
                }
                R.id.iv_revrse -> {
                    CaptureActivity.startAction(this, CardType.TYPE_ID_CARD_BACK, BaseHttp.SDPath + "img", TYPE_ID_CARD_BACK)
                    dialog.dismiss()
                }
            }
        }
        btnCancle.setOnClickListener {
            dialog.dismiss()
        }
        dialog.setContentView(view)
        dialog.show()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode== Activity.RESULT_OK){
            when(requestCode){
                TYPE_ID_CARD_FRONT -> {
                    var idCardInfo: IdCardInfo =data?.getSerializableExtra(CaptureActivity.BUNDLE_DATA) as IdCardInfo
                    ev_name.setText(idCardInfo.name)
                    ev_number.setText(idCardInfo.id)
                    Glide.with(this).load(idCardInfo.imageUrl).into(iv_frount)
                }
                TYPE_ID_CARD_BACK -> {
                    var idCardInfo: IdCardInfo =data?.getSerializableExtra(CaptureActivity.BUNDLE_DATA) as IdCardInfo
                    Glide.with(this).load(idCardInfo.imageUrl).into(iv_revrse)
                }
            }
        }
    }

    private fun selectPicture(type: Int) {
        PictureSelector.create(baseContext)
                // 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
                .openGallery(PictureMimeType.ofImage())
                // 主题样式(不设置则为默认样式)
                .theme(R.style.picture_default_style)
                // 最大图片选择数量 int
                .maxSelectNum(1)
                // 最小选择数量 int
                .minSelectNum(1)
                // 每行显示个数 int
                .imageSpanCount(4)
                // 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .selectionMode(PictureConfig.MULTIPLE)
                // 是否可预览图片 true or false
                .previewImage(true)
                // 是否可预览视频 true or false
                .previewVideo(false)
                // 是否可播放音频 true or false
                .enablePreviewAudio(false)
                // 是否显示拍照按钮 true or false
                .isCamera(true)
                // 拍照保存图片格式后缀，默认jpeg
                .imageFormat(PictureMimeType.PNG)
                // 图片列表点击 缩放效果 默认true
                .isZoomAnim(true)
                // glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                // .sizeMultiplier(0.5f)
                // 自定义拍照保存路径,可不填
                .setOutputCameraPath("zhuge_new")
                // 是否压缩 true or false
                .compress(true)
                // int glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .glideOverride(160, 160)
                // 是否裁剪 true or false
                .enableCrop(true)
                // int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                .withAspectRatio(1, 1)
                // 是否显示uCrop工具栏，默认不显示 true or false
                .hideBottomControls(true)
                // 压缩图片保存地址
                .compressSavePath(baseContext.cacheDir.absolutePath)
                // 裁剪框是否可拖拽 true or false
                .freeStyleCropEnabled(false)
                // 是否圆形裁剪 true or false
                .circleDimmedLayer(false)
                // 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                .showCropFrame(true)
                // 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                .showCropGrid(true)
                // 是否显示gif图片 true or false
                .isGif(false)
                // 是否开启点击声音 true or false
                .openClickSound(false)
                // 是否传入已选图片 List<LocalMedia> list
                .selectionMedia(selectList.apply { clear() })
                // 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                .previewEggs(true)
                // 小于100kb的图片不压缩
                .minimumCompressSize(100)
                // 结果回调onActivityResult code
                .forResult(type)
    }

}